package com.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.utils.Encrypter;

public class MailSend {

	private static Properties prop;

	/**
	 * @return properties
	 * @throws java.io.IOException
	 */
	private static Properties loadProperties() throws java.io.IOException {
		if (prop == null) {
			InputStream is = MailSend.class.getResourceAsStream("mail.properties");
			if (is != null) {
				prop = new java.util.Properties();
				prop.load(is);
				return prop;
			} else {
				return null;
			}
		}
		return prop;
	}

	public static String getContent(String input) {
		String ketqua = "";// http://24hsms.vn
		try {
			if ("".equals(ketqua) || "null".equals(ketqua) || ketqua == null) {
				ketqua = prop.getProperty("url_banner", "http://24hsms.vn");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ketqua;
	}

	public static boolean sendMail(String Subject, String MailMessage) {
		try {
			Properties props = null;
			try {
				props = loadProperties();
			} catch (IOException e) {
				e.printStackTrace();
			}

			String Host = props.getProperty("mail.smtp.host.yahoomail");// "smtp.gmail.com";
			final String username = props.getProperty("mail.smtp.user.yahoomail");// "dongtd191";
			final String password = Encrypter.decrypt(props.getProperty("mail.smtp.password.yahoomail"));
			String MailFrom = props.getProperty("mail.smtp.MailFrom.yahoomail");// "dongtd191@gmail.com";
			String MailTo = props.getProperty("mail.smtp.MailTo.yahoomail");// "xosothantai_vtm@yahoo.com.vn";
			String Port = props.getProperty("mail.smtp.port.yahoomail");
			String auth = props.getProperty("mail.smtp.auth.yahoomail");
			String fallback = props.getProperty("mail.smtp.socketFactory.fallback.yahoomail");

			// Tao mot doi tuong chua cac thuoc tinh cho
			// 1 connection toi 1 mail server
			// Goi cac thuoc tinh chuan bi cho sendmail
			Properties prop = new Properties();

			// Lay ten server
			prop.put("mail.smtp.host", Host);

			// Mail server bay gio toan doi username password thoi
			// Vi the bat smtp authentication len
			// Chung ta co su dung Username va Password
			// Vi the dat authentication = True
			prop.put("mail.smtp.auth", auth);

			// Chung ta su dung SSL cua Java
			// vi Gmail yeu cau SSL ma.
			prop.put("mail.smtp.socketFactory.class",
					"javax.net.ssl.SSLSocketFactory");
			prop.put("mail.smtp.socketFactory.fallback", fallback);

			// Gmail gui tren port 465, port default cua Mail co SSL
			prop.put("mail.smtp.port", Port);
			prop.put("mail.smtp.socketFactory.port", Port);

			// Connect toi server day, tao 1 session, dua vao password
			Session session = Session.getDefaultInstance(prop,
					new javax.mail.Authenticator() {
						protected PasswordAuthentication getPasswordAuthentication() {
							return new PasswordAuthentication(username,
									password);
						}
					});

			session.setDebug(true);

			// Tao doi tuong chua cac thuoc tinh cua mot la thu e-mail
			Message msg = new MimeMessage(session);

			// Dat dia chi mail from va to
			Address to = new InternetAddress(MailTo);
			Address from = new InternetAddress(MailFrom);

			// Roi set no vao object Message vua moi tao
			msg.setContent(MailMessage, "text/html; charset=UTF-8");
			msg.setFrom(from);
			msg.setRecipient(Message.RecipientType.TO, to);
			msg.setSubject(Subject);

			// Ra lenh send
			Transport.send(msg);
			return true;

		} catch (Exception ex) {
			System.out.println("MailSend: "+ex.toString());
			return false;
		}

	}
	
	public static boolean sendYahooMail(String alias, String mailFrom, String fromPassword, String mailTo, String Subject, String message) {
		try {
			Properties props = null;
			try {
				props = loadProperties();
			} catch (IOException e) {
				e.printStackTrace();
			}

			String Host = props.getProperty("mail.smtp.host");// "smtp.gmail.com";
			final String username = mailFrom;// "dongtd191";
			final String password = fromPassword;
			String MailFrom = "\""+alias+"\"<" + mailFrom + "@yahoo.com>";// "dongtd191@gmail.com";
			String Port = props.getProperty("mail.smtp.port");
			String auth = props.getProperty("mail.smtp.auth");
			String fallback = props.getProperty("mail.smtp.socketFactory.fallback");

			// Tao mot doi tuong chua cac thuoc tinh cho
			// 1 connection toi 1 mail server
			// Goi cac thuoc tinh chuan bi cho sendmail
			Properties prop = new Properties();

			// Lay ten server
			prop.put("mail.smtp.host", Host);

			// Mail server bay gio toan doi username password thoi
			// Vi the bat smtp authentication len
			// Chung ta co su dung Username va Password
			// Vi the dat authentication = True
			prop.put("mail.smtp.auth", auth);

			// Chung ta su dung SSL cua Java
			// vi Gmail yeu cau SSL ma.
			prop.put("mail.smtp.socketFactory.class",
					"javax.net.ssl.SSLSocketFactory");
			prop.put("mail.smtp.socketFactory.fallback", fallback);

			// Gmail gui tren port 465, port default cua Mail co SSL
			prop.put("mail.smtp.port", Port);
			prop.put("mail.smtp.socketFactory.port", Port);

			// Connect toi server day, tao 1 session, dua vao password
			Session session = Session.getDefaultInstance(prop,
					new javax.mail.Authenticator() {
						protected PasswordAuthentication getPasswordAuthentication() {
							return new PasswordAuthentication(username,
									password);
						}
					});

			session.setDebug(true);

			// Tao doi tuong chua cac thuoc tinh cua mot la thu e-mail
			Message msg = new MimeMessage(session);

			// Dat dia chi mail from va to
			Address to = new InternetAddress(mailTo);
			Address from = new InternetAddress(MailFrom);

			// Roi set no vao object Message vua moi tao
			msg.setContent(message, "text/html; charset=UTF-8");
			msg.setFrom(from);
			msg.setRecipient(Message.RecipientType.TO, to);
			msg.setSubject(Subject);

			// Ra lenh send
			Transport.send(msg);
			return true;

		} catch (Exception ex) {
			System.out.println("MailSend: "+ex.toString());
			return false;
		}

	}

	public static void main(String[] args) {
		MailSend.sendYahooMail("NoReply", "nthienphuong119", "@ngay11thang9", "nthienphuong@gmail.com", "Raovat.vn", "Xin giới thiệu");
	}

}
